package org.jeecg.modules.xl.order.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.exception.JeecgBootException;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.xl.order.dto.XlCustomerOrdeDTO;
import org.jeecg.modules.xl.order.entity.XlCustomerOrde;
import org.jeecg.modules.xl.order.entity.XlCustomerShipment;
import org.jeecg.modules.xl.order.mapper.XlCustomerOrdeMapper;
import org.jeecg.modules.xl.order.mapper.XlCustomerShipmentDetailMapper;
import org.jeecg.modules.xl.order.mapper.XlCustomerShipmentMapper;
import org.jeecg.modules.xl.order.service.IXlCustomerShipmentService;
import org.jeecg.modules.xl.order.utils.DictUtils;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @Description: 发货表
 * @Author: jeecg-boot
 * @Date:   2024-10-27
 * @Version: V1.0
 */
@Service
public class XlCustomerShipmentServiceImpl extends ServiceImpl<XlCustomerShipmentMapper, XlCustomerShipment> implements IXlCustomerShipmentService {

	@Autowired
	private XlCustomerShipmentMapper xlCustomerShipmentMapper;
	@Autowired
	private XlCustomerShipmentDetailMapper xlCustomerShipmentDetailMapper;
	@Autowired
	private DictUtils dictUtils;
	
	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delMain(String id) {
		xlCustomerShipmentDetailMapper.deleteByMainId(id);
		xlCustomerShipmentMapper.deleteById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			xlCustomerShipmentDetailMapper.deleteByMainId(id.toString());
			xlCustomerShipmentMapper.deleteById(id);
		}
	}

	@Override
	public void takeDeliveryByDeliveryCode(String deliveryCode) {
		//当前操作人
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();

		XlCustomerShipment orde = this.getOne(Wrappers.lambdaQuery(XlCustomerShipment.class)
			.eq(XlCustomerShipment::getDeliveryCode, deliveryCode)
		);
		if (orde != null){
			//判断是否未已收货
			if (orde.getState() != null && dictUtils.getDictValue("customer_shipment_state","已收货").equals(orde.getState())) {
				throw new JeecgBootException("该发货单:"+orde.getOrderCode()+"已收货,不能重复修改！");
			}
		}else{
			throw new JeecgBootException("该发货单快递号找不到对应数据：" + deliveryCode);
		}

		//修改为已收货
		this.update(Wrappers.lambdaUpdate(XlCustomerShipment.class)
				.set(XlCustomerShipment::getState, dictUtils.getDictValue("customer_shipment_state","已收货"))
				.set(XlCustomerShipment::getTakeDeliveryTime, new Date())
				.set(XlCustomerShipment::getTakeDeliveryBy, user.getUsername())
				.eq(XlCustomerShipment::getDeliveryCode, deliveryCode)
		);
	}

	@Override
	public void takeDelivery(XlCustomerOrdeDTO dto) {
		//当前操作人
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<String> idList = Lists.newArrayList();
		//判断是批量还是单个修改
		if (dto.getId().indexOf(",") > -1) {
			idList.addAll(Arrays.asList(dto.getId().split(",")));

			List<XlCustomerShipment> list = this.listByIds(idList);
			//判断是否都查询到
			if (list.size() == idList.size()) {
				//筛选判断是否合规核的数据
				String shipmentCodes = list.stream().filter(f -> f.getState() == null || !dictUtils.getDictValue("customer_shipment_state","收货中").equals(f.getState()))
						.map(m -> m.getShipmentCode()).collect(Collectors.joining(","));
				String takeDeliveryCodes = list.stream().filter(f -> f.getState() != null && dictUtils.getDictValue("customer_shipment_state","已收货").equals(f.getState()))
						.map(m -> m.getShipmentCode()).collect(Collectors.joining(","));

				//判断是否未提交收货凭证
				if (shipmentCodes.length() > 0) {
					throw new JeecgBootException("发货单【"+shipmentCodes+"]未提交收货凭证,不能操作！");
					//判断是否已收货
				}else if (takeDeliveryCodes.length() > 0) {
					throw new JeecgBootException("发货单【"+takeDeliveryCodes+"]已收货,不能操作！");
				}
			}else{
				throw new JeecgBootException("选择的数据中有无效数据,请刷新后重新操作");
			}
		}else{
			XlCustomerShipment orde = this.getById(dto.getId());
			if (orde != null){
				idList.add(orde.getId()+"");

				//判断是否未提交收货凭证或已收货
				if (orde.getState() == null || !dictUtils.getDictValue("customer_shipment_state","收货中").equals(orde.getState())) {
					throw new JeecgBootException("未提交收货凭证的订单,不能操作！");
				}else if (orde.getState() != null && dictUtils.getDictValue("customer_shipment_state","已收货").equals(orde.getState())) {
					throw new JeecgBootException("该发货单:"+orde.getOrderCode()+"已收货,不能操作！");
				}else{
				}
			}else{
				throw new JeecgBootException("该发货单id找不到对应数据："+dto.getId());
			}
		}

		//修改为已收货
		this.update(Wrappers.lambdaUpdate(XlCustomerShipment.class)
				.set(XlCustomerShipment::getState, dictUtils.getDictValue("customer_shipment_state","已收货"))
				.set(XlCustomerShipment::getTakeDeliveryTime, new Date())
				.set(XlCustomerShipment::getTakeDeliveryBy, user.getUsername())
				.in(XlCustomerShipment::getId, idList)
		);
	}


	@Override
	public void submitTakeDelivery(XlCustomerOrdeDTO dto) {
		//当前操作人
		LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<String> idList = Lists.newArrayList();
		//判断是批量还是单个修改
		if (dto.getId().indexOf(",") > -1) {
			idList.addAll(Arrays.asList(dto.getId().split(",")));

			List<XlCustomerShipment> list = this.listByIds(idList);
			//判断是否都查询到
			if (list.size() == idList.size()) {
				//筛选判断是否合规核的数据
				String takeDeliveryCodes = list.stream().filter(f -> f.getState() != null && dictUtils.getDictValue("customer_shipment_state","收货中").equals(f.getState()))
						.map(m -> m.getShipmentCode()).collect(Collectors.joining(","));

				//判断是否提交收货凭证
				if (takeDeliveryCodes.length() > 0) {
					throw new JeecgBootException("发货单【"+takeDeliveryCodes+"]已提交收货凭证,不能操作！");
				}
			}else{
				throw new JeecgBootException("选择的数据中有无效数据,请刷新后重新操作");
			}
		}else{
			XlCustomerShipment orde = this.getById(dto.getId());
			if (orde != null){
				idList.add(orde.getId()+"");
				//判断是否未发货或收货中
				if (orde.getState() != null && dictUtils.getDictValue("customer_shipment_state","收货中").equals(orde.getState())) {
					throw new JeecgBootException("该发货单:"+orde.getOrderCode()+"已提交收货凭证,不能操作！");
				}else{
				}
			}else{
				throw new JeecgBootException("该发货单id找不到对应数据："+dto.getId());
			}
		}

		//修改为收货中
		this.update(Wrappers.lambdaUpdate(XlCustomerShipment.class)
				.set(XlCustomerShipment::getState, dictUtils.getDictValue("customer_shipment_state","收货中"))
				.set(XlCustomerShipment::getTakeDeliveryImage, dto.getImage())
				.set(XlCustomerShipment::getTakeDeliveryTime, new Date())
				.set(XlCustomerShipment::getTakeDeliveryBy, user.getUsername())
				.in(XlCustomerShipment::getId, idList)
		);
	}
	
}
